package cn.migu.music.hangupsms.repository;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import cn.migu.music.hangupsms.common.entity.UserSendMsgInfo;

/**
 * 用户短信发送记录查询
 **/
@Mapper
public interface HangupMsgSendUserMapper {

    @Insert(value = "insert into t_migu_hangupmsgsenduser (phonenumber,sendmsgtime,localid,msgtemplateid) values (#{info.phonenumber},#{info.sendmsgtime},#{info.localid},#{info.msgtemplateid})")
    void save(@Param("info") UserSendMsgInfo info);

    @Select(value = "select phonenumber,sendmsgtime,localid,msgtemplateid from t_migu_hangupmsgsenduser where phonenumber=#{phonenumber} and localid=#{localid}")
    List<UserSendMsgInfo> querySendMsgUserInfo(@Param("phonenumber") String phonenumber, @Param("localid") Integer localId);

    @Select(value = "select phonenumber,sendmsgtime from t_migu_hangupmsgsenduser where localid=#{localid} order by sendmsgtime limit #{pageSize} offset #{pageStart}")
    List<UserSendMsgInfo> pageQuery(@Param("localid") Integer localId, @Param("pageSize") int pageSize, @Param("pageStart") int pageStart);

    @Select(value = "select count(*) from t_migu_hangupmsgsenduser where localid=#{localid} and msgtemplateid=#{templateId} " +
                    "and sendmsgtime between #{startTime} and #{endTime}")
    int getSendCount(@Param("localid") Integer localId,
                     @Param("templateId") String templateId,
                     @Param("startTime") Date startTime,
                     @Param("endTime") Date endTime);

    @Select(value = "select count(*) from t_migu_hangupmsgsenduser where msgtemplateid=#{templateId} " +
                    "and sendmsgtime between #{startTime} and #{endTime}")
    int getSendCountNoLocalId(@Param("templateId") String templateId,
                              @Param("startTime") Date startTime,
                              @Param("endTime") Date endTime);

    @Select(value = "select phonenumber,sendmsgtime,localid,msgtemplateid from t_migu_hangupmsgsenduser where phonenumber=#{phonenumber} and  localid=#{localid} and sendmsgtime between #{starttime} and  #{endtime} order by sendmsgtime")
    List<UserSendMsgInfo> pageQuerySendMsgUserInfo(@Param("phonenumber") String phonenumber,
                                                   @Param("starttime") Timestamp starttime,
                                                   @Param("endtime") Timestamp endtime,
                                                   @Param("localid") Integer localId);

    @Select(value = "select phonenumber,sendmsgtime,localid,msgtemplateid from t_migu_hangupmsgsenduser where phonenumber=#{phonenumber} and  localid=#{localid}  order by sendmsgtime")
    List<UserSendMsgInfo> pageQuerySendMsgUserInfoWithoutTime(@Param("phonenumber") String phonenumber,
                                                              @Param("localid") Integer localId);

}
